﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using FazaDemo.ViewModels;

namespace FazaDemo.PL.Wpf
{
    public class WebApiClient
    {

        private readonly ApiRouteFactory _factory;

        public WebApiClient()
        {
            _factory = new ApiRouteFactory();
        }

        public IEnumerable<T> GetList<T>()
        {
            var client = new HttpClient { BaseAddress = new Uri("http://172.19.195.10:8380") };
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            try
            {
                HttpResponseMessage response = client.GetAsync(_factory.GetRoute<T>()).Result;
                response.EnsureSuccessStatusCode();
                return response.Content.ReadAsAsync<IEnumerable<T>>().Result.ToList();
            }
            catch (Exception)
            {
                return default(IEnumerable<T>);
            }
        }

    }
}
